package com.heima.exam.p4;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Demo1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException {
        FileInputStream fis = new FileInputStream("src/com/heima/exam/p4/stu.properties");
        Properties p = new Properties();
        p.load(fis);
        String className = p.getProperty("className");
        p.remove("className");
        Class<?> aClass = Class.forName(className);
        Constructor<?> constructor = aClass.getConstructor(String.class, int.class);
        ArrayList<String> al = new ArrayList<>();
        Set<String> strings = p.stringPropertyNames();
        for (String string : strings) {
            String property = p.getProperty(string);
            al.add(property);
        }
        Set<Student> collect = al.stream().limit(3).map(new Function<String, Student>() {
            @Override
            public Student apply(String s) {
                String[] split = s.split(",");
                try {
                    return (Student) constructor.newInstance(split[0], Integer.parseInt(split[1]));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).collect(Collectors.toSet());
        TreeSet<Student> tr = new TreeSet<>((a, b) -> a.age > b.age ? -1 : 1);
        tr.addAll(collect);
        Student first = tr.first();
        System.out.println(first);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("src/com/heima/exam/p4/stumax.txt"));

    }
}
